home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJTST200.ZIP / tests / libc / ansi / stdio / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-05  |  4.2 KB  |  200 lines

  1. #include <stdio.h>
  2. #ifdef __DJGPP__
  3. #include <libc/file.h>
  4. #endif
  5. #include <io.h>
  6.  
  7. const char *rfname = "file.c";
  8. const char *wfname = "filetemp.dat";
  9.  
  10. int enable_read = 0;
  11. int enable_write = 0;
  12. int enable_text = 0;
  13. int enable_binary = 0;
  14.  
  15. void
  16. do_headers(void)
  17. {
  18.   printf(" ftell   tell  flags  cnt     ptr     base    bufsiz   remarks  \n");
  19.   printf("------- ------ ----- ----- -------- -------- -------- --------- \n");
  20.    
  21. }
  22.  
  23. void
  24. debug_file(FILE *f, const char *remarks)
  25. {
  26.   printf("%7ld %6d ", ftell(f), tell(fileno(f)));
  27. #ifdef __DJGPP__
  28.   putchar(ferror(f) ? 'E' : ' ');
  29.   putchar(f->_flag & 001000  ? 'M' : ' ');
  30.   putchar(f->_flag & 000020  ? 'W' : ' ');
  31.   putchar(f->_flag & 000010  ? 'R' : ' ');
  32.   printf("  %5d %7p %8p %8d", f->_cnt, f->_ptr, f->_base, f->_bufsiz);
  33. #endif
  34.   printf("  %s\n", remarks);
  35. }
  36.  
  37. void
  38. must(int a, int b)
  39. {
  40.   if (a != b)
  41.     printf("*** warning - read %d, not %d\n", a, b);
  42. }
  43.  
  44. void
  45. do_mode(const char *rmode, const char *wmode, const char *description)
  46. {
  47.   char line[2048], c;
  48.   FILE *f;
  49.   int j;
  50.  
  51.   if (enable_read)
  52.   {
  53.     f = fopen(rfname, rmode);
  54.     setvbuf(f, 0, _IOFBF , 512);
  55.  
  56. #ifdef __DJGPP__
  57.     printf("\n---- %s File, Read, bufsiz=%d ----\n", description, f->_bufsiz);
  58.     do_headers();
  59. #endif
  60.  
  61.     debug_file(f, "After fopen");
  62.  
  63.     fgetc(f);
  64.     debug_file(f, "After fgetc");
  65.  
  66.     fgets(line, 2048, f);
  67.     debug_file(f, "After fgets");
  68.  
  69.     fseek(f, 5, SEEK_CUR);
  70.     debug_file(f, "After fseek cur+5");
  71.  
  72.     c = fgetc(f);
  73.     sprintf(line, "After fgetc -> %d", c);
  74.     debug_file(f, line);
  75.  
  76.     fseek(f, 0x6b3, SEEK_SET);
  77.     debug_file(f, "After fseek 0x6b3");
  78.  
  79.     fread(line, 1, 10, f);
  80.     for (j=0; j<10; j++)
  81.       printf(" %02x", line[j]);
  82.     printf("\n");
  83.  
  84. #ifdef __DJGPP__
  85.     fseek(f, 0, SEEK_SET);
  86.     fgetc(f);
  87.     while (f->_cnt)
  88.       fgetc(f);
  89.     debug_file(f, "after reading a full buffer");
  90. #endif
  91.  
  92.     fseek(f, 0, SEEK_SET);
  93.     must(fread(line, 1, 600, f), 600);
  94.     must(fread(line, 1, 600, f), 600);
  95.     debug_file(f, "after reading 1200 from beginning");
  96.     fread(line, 10, 1, f);
  97.     debug_file(f, "then read 10");
  98.     for (j=0; j<10; j++)
  99.       printf(" %02x", line[j]);
  100.     printf("\n");
  101.  
  102.     fseek(f, 0, SEEK_SET);
  103.     must(fread(line, 1, 600, f), 600);
  104.     must(fread(line, 1, 600, f), 600);
  105.     debug_file(f, "re-start");
  106.     j = ftell(f);
  107.     debug_file(f, "ftell");
  108.     fseek(f, j, SEEK_SET);
  109.     debug_file(f, "fseek there");
  110.     fread(line, 1, 10, f);
  111.     debug_file(f, "re-read");
  112.     for (j=0; j<10; j++)
  113.       printf(" %02x", line[j]);
  114.     printf("\n");
  115.  
  116.     fclose(f);
  117.   }
  118.  
  119.   if (enable_write)
  120.   {
  121.     f = fopen(wfname, wmode);
  122.     setvbuf(f, 0, _IOFBF , 512);
  123.  
  124. #ifdef __DJGPP__
  125.     printf("\n---- %s File, Write, bufsiz=%d ----\n", description, f->_bufsiz);
  126.     do_headers();
  127. #endif
  128.  
  129.     debug_file(f, "After fopen");
  130.  
  131.     fputc('x', f);
  132.     debug_file(f, "After fputc x");
  133.  
  134.     fputs("hello", f);
  135.     debug_file(f, "After fputs hello");
  136.  
  137.     fputc('\n', f);
  138.     debug_file(f, "After fputc NL");
  139.  
  140.     fputs("jfkldsjakl;fdsaj;\nfdjksla;jfkldsja\nfjkdsl;ajfkdls;ajkfds\nfjdks;jafkld;sa\n", f);
  141.     debug_file(f, "After fputs long");
  142.  
  143.     fflush(f);
  144.     debug_file(f, "After fflush");
  145.  
  146.     fseek(f, 15, SEEK_SET);
  147.     debug_file(f, "After fseek 15");
  148.  
  149.     fputc('x', f);
  150.     debug_file(f, "After fputc x");
  151.  
  152.     fseek(f, 1, SEEK_CUR);
  153.     debug_file(f, "After fseek cur+1");
  154.  
  155.     fflush(f);
  156.     debug_file(f, "After fflush");
  157.  
  158.     fclose(f);
  159.   }
  160.  
  161. }
  162.  
  163. int
  164. main(int argc, char **argv)
  165. {
  166.   while (argc > 1 && argv[1][0] == '-')
  167.   {
  168.     switch (argv[1][1]) {
  169.     case 'r':
  170.       enable_read = 1;
  171.       break;
  172.     case 'w':
  173.       enable_write = 1;
  174.       break;
  175.     case 'b':
  176.       enable_binary = 1;
  177.       break;
  178.     case 't':
  179.       enable_text = 1;
  180.       break;
  181.     }
  182.     argc--;
  183.     argv++;
  184.   }
  185.   if (argc > 1)
  186.     rfname = argv[1];
  187.  
  188.   if (!enable_binary && !enable_text)
  189.     enable_binary = enable_text = 1;
  190.   if (!enable_read && !enable_write)
  191.     enable_read = enable_write = 1;
  192.  
  193.   if (enable_binary)
  194.     do_mode("rb", "wb", "Binary");
  195.   if (enable_text)
  196.     do_mode("rt", "wt", "Text");
  197.   remove(wfname);
  198.   return 0;
  199. }
  200.